/*
 * PrincipalForm.java
 *
 * Created on 26 de Agosto de 2006, 15:36
 */

package info.carl.jgdt.gui;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.swing.EventListModel;
import info.carl.jgdt.beans.Task;
import info.carl.jgdt.dao.TaskDao;
import info.carl.jgdt.dao.TaskDaoImpl;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.LookAndFeel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import jgdt.Main;

/**
 *
 * @author  cliente
 */
public class PrincipalForm extends javax.swing.JFrame {
    
    TaskDao taskDao;
    Task activeTask;
    EventList<Task> taskList;
    EventListModel taskListModel;
    EditStatus editStatus;
    /** Creates new form PrincipalForm */
    public void openFile(String fileName){
        taskDao = new TaskDaoImpl(fileName);
        updateProjectsList(null);
        updateContextsList(null);
        updateTasksList(null);
        Properties prop = new Properties();
        prop.setProperty("fileName", fileName);
        setTitle(Main.appName + " " + Main.version + " - " + fileName);
        try{
            prop.store(new FileOutputStream("settings.prop"), "");
        }catch(Exception e){
            e.printStackTrace();
        }
        
    }
    
    public void setTaskStatus(EditStatus status){
        String strStatus = java.util.ResourceBundle.getBundle("resources").getString("Details");
        editStatus = status;    
        switch(status){
            case BROWSE: strStatus = java.util.ResourceBundle.getBundle("resources").getString("Details"); break;
            case INSERT: strStatus = java.util.ResourceBundle.getBundle("resources").getString("Details_Inserting_new_task"); break;
            case EDIT: strStatus = java.util.ResourceBundle.getBundle("resources").getString("Details_Editing_Task"); break;
            
        }
        jPanel2.setBorder(BorderFactory.createTitledBorder(strStatus));        
        lbProjects.setEnabled(status==EditStatus.BROWSE); 
        lbContexts.setEnabled(status==EditStatus.BROWSE);
        lbTasks.setEnabled(status==EditStatus.BROWSE);
        cbProject.setEnabled(status!=EditStatus.BROWSE);
        cbContext.setEnabled(status!=EditStatus.BROWSE);
        edDescription.setEnabled(status!=EditStatus.BROWSE);
        btnNovo.setEnabled(status==EditStatus.BROWSE);
        btnEdit.setEnabled(status==EditStatus.BROWSE);
        btnDelete.setEnabled(status==EditStatus.BROWSE);
        btnSave.setEnabled(status!=EditStatus.BROWSE);
        btnCancel.setEnabled(status!=EditStatus.BROWSE);
        
    }
    public void updateProjectsList(String selecionado){
        DefaultListModel model = new DefaultListModel();
        DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
        int index = 0;
        for (String project: taskDao.getProjects()){
            model.add(index++, project);
            comboModel.addElement(project);
            
        }
        lbProjects.setModel(model);
        if (selecionado != null)
            lbProjects.setSelectedValue(selecionado, true);
        else
            lbProjects.setSelectedIndex(0);
        if (selectAllProjects.isSelected()){
            lbProjects.setSelectionInterval(0, lbProjects.getModel().getSize()-1);
        }
        cbProject.setModel(comboModel);
    }
    
    public void clearDetails(){
        cbProject.setSelectedItem("");
        cbContext.setSelectedItem("");
        edDescription.setText("");
        
    }
        
    public void updateDetails(Task task){
        clearDetails();
        if (task != null){
            cbProject.setSelectedItem(task.getProject());
            cbContext.setSelectedItem(task.getContext());
            edDescription.setText(task.getDescription());
            activeTask = task;
        }
    }
    
    public void updateTasksList(Task selectedTask){
        int indices[] = lbProjects.getSelectedIndices();
        List<String> selectedProjects = new ArrayList<String>();
        List<String> selectedContexts = new ArrayList<String>();
        for (int index = 0; index < indices.length; index++){
            selectedProjects.add((String)lbProjects.getModel().getElementAt(indices[index]));
        }
        indices = lbContexts.getSelectedIndices();
        for (int index = 0; index < indices.length; index++){
            selectedContexts.add((String)lbContexts.getModel().getElementAt(indices[index]));
        }
        taskList.clear();
        taskList.addAll(taskDao.getTasks(selectedProjects, selectedContexts));
        
        if (selectedTask == null)
          lbTasks.setSelectedIndex(0);
        else
          lbTasks.setSelectedValue(selectedTask, true)  ;
        updateDetails((Task)lbTasks.getSelectedValue());
                
    }
    public void updateContextsList(String selecionado){
        DefaultListModel model = new DefaultListModel();
        DefaultComboBoxModel comboModel = new DefaultComboBoxModel();
        int index = 0;
        for (String context: taskDao.getContexts()){
            model.add(index++, context);
            comboModel.addElement(context);
        }
        lbContexts.setModel(model);
        if (selecionado != null)
            lbContexts.setSelectedValue(selecionado, true);
        else
            lbContexts.setSelectedIndex(0);
        if (selectAllContexts.isSelected()){
            lbContexts.setSelectionInterval(0, lbContexts.getModel().getSize()-1);
        }

        cbContext.setModel(comboModel);
        cbContext.setSelectedItem(selecionado);
    }
    
    
    
    public PrincipalForm() {
        taskList = new BasicEventList();
        taskListModel = new EventListModel(taskList);
        initComponents();
        setTitle(Main.appName + " " + Main.version);
        Properties prop = new Properties();
        String fileName = "";
        try{
            prop.load(new FileInputStream("settings.prop"));    
            fileName = prop.getProperty("fileName");
        }catch(FileNotFoundException e){
            //The settings file was not found, ask for a file and save the properties
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
            fileChooser.showDialog(this, "Set Active File");
            fileName = fileChooser.getSelectedFile().getAbsolutePath();
        }catch(IOException e){
            JOptionPane.showMessageDialog(this, e.getMessage());
            System.exit(-1);
        }
        openFile(fileName);
        setTaskStatus(EditStatus.BROWSE);
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lbProjects = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        lbContexts = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        cbProject = new javax.swing.JComboBox();
        cbContext = new javax.swing.JComboBox();
        edDescription = new javax.swing.JTextField();
        btnSave = new javax.swing.JButton();
        btnNovo = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        lbTasks = new javax.swing.JList();
        selectAllProjects = new javax.swing.JCheckBox();
        selectAllContexts = new javax.swing.JCheckBox();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(java.util.ResourceBundle.getBundle("resources").getString("jgdt_1.0_-_get_things_done"));
        jLabel1.setText(java.util.ResourceBundle.getBundle("resources").getString("Projects"));

        lbProjects.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lbProjectsValueChanged(evt);
            }
        });
        lbProjects.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbProjectsMouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(lbProjects);

        jLabel2.setText(java.util.ResourceBundle.getBundle("resources").getString("Contexts"));

        lbContexts.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lbContextsValueChanged(evt);
            }
        });
        lbContexts.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbContextsMouseClicked(evt);
            }
        });

        jScrollPane2.setViewportView(lbContexts);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(java.util.ResourceBundle.getBundle("resources").getString("Details")));
        jLabel3.setText(java.util.ResourceBundle.getBundle("resources").getString("Project"));

        jLabel4.setText(java.util.ResourceBundle.getBundle("resources").getString("Context"));

        jLabel5.setText(java.util.ResourceBundle.getBundle("resources").getString("Description"));

        cbProject.setEditable(true);
        cbProject.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbProjectItemStateChanged(evt);
            }
        });
        cbProject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbProjectActionPerformed(evt);
            }
        });
        cbProject.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                cbProjectFocusLost(evt);
            }
        });

        cbContext.setEditable(true);
        cbContext.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbContextItemStateChanged(evt);
            }
        });
        cbContext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbContextActionPerformed(evt);
            }
        });
        cbContext.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                cbContextFocusLost(evt);
            }
        });

        edDescription.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edDescriptionActionPerformed(evt);
            }
        });
        edDescription.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                edDescriptionKeyPressed(evt);
            }
        });

        btnSave.setText(java.util.ResourceBundle.getBundle("resources").getString("Save"));
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnNovo.setText(java.util.ResourceBundle.getBundle("resources").getString("New"));
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        btnEdit.setText(java.util.ResourceBundle.getBundle("resources").getString("Edit"));
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnDelete.setText(java.util.ResourceBundle.getBundle("resources").getString("Delete"));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        btnCancel.setText(java.util.ResourceBundle.getBundle("resources").getString("Cancel"));
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .add(btnNovo)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnEdit)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnDelete)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 110, Short.MAX_VALUE)
                        .add(btnCancel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(btnSave)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .add(jLabel5))
                            .add(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .add(jLabel4))
                            .add(jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .add(jLabel3)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(cbProject, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 142, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(cbContext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 142, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(edDescription, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cbProject, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cbContext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(edDescription, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnDelete)
                    .add(btnEdit)
                    .add(btnNovo)
                    .add(btnCancel)
                    .add(btnSave))
                .addContainerGap())
        );

        lbTasks.setModel(taskListModel);
        lbTasks.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lbTasksValueChanged(evt);
            }
        });
        lbTasks.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbTasksMouseClicked(evt);
            }
        });

        jScrollPane4.setViewportView(lbTasks);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 492, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .add(jScrollPane4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );

        selectAllProjects.setText(java.util.ResourceBundle.getBundle("resources").getString("Select_All"));
        selectAllProjects.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        selectAllProjects.setMargin(new java.awt.Insets(0, 0, 0, 0));
        selectAllProjects.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectAllProjectsActionPerformed(evt);
            }
        });

        selectAllContexts.setText(java.util.ResourceBundle.getBundle("resources").getString("Select_All"));
        selectAllContexts.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        selectAllContexts.setMargin(new java.awt.Insets(0, 0, 0, 0));
        selectAllContexts.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectAllContextsActionPerformed(evt);
            }
        });

        jMenu1.setMnemonic('F');
        jMenu1.setText(java.util.ResourceBundle.getBundle("resources").getString("File"));
        jMenuItem2.setText(java.util.ResourceBundle.getBundle("resources").getString("Set_Active_File..."));
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem2);

        jMenuItem1.setMnemonic('q');
        jMenuItem1.setText(java.util.ResourceBundle.getBundle("resources").getString("Quit"));
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });

        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(selectAllProjects))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane1, 0, 0, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(selectAllContexts))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(selectAllProjects))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel2)
                            .add(selectAllContexts))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 183, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lbTasksMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbTasksMouseClicked
      if (evt.getClickCount() == 2){
          if (editStatus == EditStatus.BROWSE){
            btnEditActionPerformed(null);
          }
          
      }
    }//GEN-LAST:event_lbTasksMouseClicked

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        if (JOptionPane.showConfirmDialog(this, "Do you really want to delete this task?","Confirmation",  JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
            try{
                taskDao.delete(activeTask);
            }catch(Exception e){
                new ExceptionDialog(this, e);
            }
            activeTask = new Task();
            updateProjectsList(null);
            updateContextsList(null);
            updateTasksList(null);
            setTaskStatus(EditStatus.BROWSE);
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        setTaskStatus(EditStatus.BROWSE);
        updateDetails(activeTask);
    }//GEN-LAST:event_btnCancelActionPerformed

    private void selectAllContextsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllContextsActionPerformed
        if (selectAllContexts.isSelected()){
            lbContexts.setSelectionInterval(0, lbContexts.getModel().getSize()-1);
        }else{
            lbContexts.setSelectedIndex(0);
        }
    }//GEN-LAST:event_selectAllContextsActionPerformed

    private void selectAllProjectsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectAllProjectsActionPerformed
        if (selectAllProjects.isSelected()){
            lbProjects.setSelectionInterval(0, lbProjects.getModel().getSize()-1);
        }else{
            lbProjects.setSelectedIndex(0);
        }
    }//GEN-LAST:event_selectAllProjectsActionPerformed

    private void lbTasksValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lbTasksValueChanged
        if (lbTasks.getSelectedValue() != null)
            updateDetails((Task)lbTasks.getSelectedValue());
        else
            clearDetails();
    }//GEN-LAST:event_lbTasksValueChanged

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        setTaskStatus(EditStatus.EDIT);
        cbProject.requestFocus();
                
    }//GEN-LAST:event_btnEditActionPerformed

    private void cbContextItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbContextItemStateChanged
 
    }//GEN-LAST:event_cbContextItemStateChanged

    private void edDescriptionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_edDescriptionKeyPressed
 
    }//GEN-LAST:event_edDescriptionKeyPressed

    private void edDescriptionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edDescriptionActionPerformed
        
    }//GEN-LAST:event_edDescriptionActionPerformed

    private void cbContextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbContextActionPerformed
        if (activeTask == null) return;
        if (cbContext.getSelectedItem() == null) return;
            String text = (String)cbContext.getSelectedItem();
          if (text.equals("")) return;
          if (!text.startsWith("@")){
              text = "@".concat(text);
              cbContext.setSelectedItem(text);
          }
    }//GEN-LAST:event_cbContextActionPerformed

    private void cbProjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbProjectActionPerformed
        if (activeTask == null) return;
        if (cbProject.getSelectedItem() == null) return;
          String text = (String)cbProject.getSelectedItem();
          if (text.equals("")) return;
          if (!text.startsWith("+")){
              text = "+".concat(text);
              cbProject.setSelectedItem(text);
          }
          
    }//GEN-LAST:event_cbProjectActionPerformed

    private void cbProjectItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbProjectItemStateChanged

    }//GEN-LAST:event_cbProjectItemStateChanged

    private void cbContextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cbContextFocusLost
      

    }//GEN-LAST:event_cbContextFocusLost

    private void cbProjectFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cbProjectFocusLost
      
    }//GEN-LAST:event_cbProjectFocusLost

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        activeTask = new Task();
        activeTask.setId(0);
        setTaskStatus(EditStatus.INSERT);
        updateDetails(activeTask);
        cbProject.requestFocus();
        
    }//GEN-LAST:event_btnNovoActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        activeTask.setContext((String)cbContext.getSelectedItem());
        activeTask.setProject((String)cbProject.getSelectedItem());
        activeTask.setDescription(edDescription.getText());
        try{
            taskDao.save(activeTask);
        }catch(Exception e){
            new ExceptionDialog(this, e);
            return;
        }    
        
        updateProjectsList(activeTask.getProject());
        updateContextsList(activeTask.getContext());
        updateTasksList(activeTask);
        setTaskStatus(EditStatus.BROWSE);
    }//GEN-LAST:event_btnSaveActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
        fileChooser.showDialog(this, "Set Active File");
        if (fileChooser.getSelectedFile() != null){
            openFile(fileChooser.getSelectedFile().getAbsolutePath());
        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void lbContextsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbContextsMouseClicked
        if (evt.getClickCount() == 2){
            lbProjects.setSelectionInterval(0, lbProjects.getModel().getSize()-1);
        }
    }//GEN-LAST:event_lbContextsMouseClicked

    private void lbProjectsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbProjectsMouseClicked
        if (evt.getClickCount() == 2){
            lbContexts.setSelectionInterval(0, lbContexts.getModel().getSize()-1);
        }
    }//GEN-LAST:event_lbProjectsMouseClicked

    private void lbContextsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lbContextsValueChanged
        if (editStatus == EditStatus.BROWSE)
            updateTasksList(null);
    }//GEN-LAST:event_lbContextsValueChanged

    private void lbProjectsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lbProjectsValueChanged
        if (editStatus == EditStatus.BROWSE)
            updateTasksList(null);
    }//GEN-LAST:event_lbProjectsValueChanged
    
    /**
     * @param args the command line arguments
     */
   
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cbContext;
    private javax.swing.JComboBox cbProject;
    private javax.swing.JTextField edDescription;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JList lbContexts;
    private javax.swing.JList lbProjects;
    private javax.swing.JList lbTasks;
    private javax.swing.JCheckBox selectAllContexts;
    private javax.swing.JCheckBox selectAllProjects;
    // End of variables declaration//GEN-END:variables
    
    private enum EditStatus{
        BROWSE (0), EDIT (1), INSERT (2);
        private final int value;
        EditStatus(int value){
            this.value = value;
        }

        public int getValue() {
            return value;
        }
    }
    
    
}
